package com.ikun.controller;

import com.ikun.pojo.Result;
import com.ikun.pojo.TbComment;
import com.ikun.pojo.TbTask;
import com.ikun.pojo.TbUser;
import com.ikun.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    CommentService commentService;

    //查询我的评论
    @RequestMapping("selMyComment")
    @ResponseBody
    public Result selMyComment(HttpServletRequest request, HttpServletResponse response){
        int userid = Integer.parseInt(request.getParameter("id"));
        int page = Integer.parseInt(request.getParameter("pg"));
        List<TbComment> commentList = commentService.selMyComment(userid,page);
        int count = commentService.selMyCommentCount(userid);
        Result result = new Result();
        if(commentList==null || commentList.size()==0){
            result.setMessage("fail");
        }else{
            result.setMessage("success");
        }
        result.setList(commentList);
        result.setCode(count);
        return result;
    }


    //查询评论
    @RequestMapping("selComment")
    @ResponseBody
    public Result selComment(String taskid){
        List<TbComment> tbComments = commentService.selComment(Integer.parseInt(taskid));
        System.out.println(tbComments.size()+"abab");
        Result result = new Result();
        result.setList(tbComments);
        return result;
    }

    //添加评论
    @RequestMapping("addComment")
    @ResponseBody
    public Result addComment(String userid,String taskid,String commentid,String text){
        Long comid = 0L;
        if(null != commentid && "" != commentid){
            comid = Long.parseLong(commentid);
        }
        System.out.println(userid+"/"+taskid+"/"+commentid+"/"+text);
        TbUser tbUser = new TbUser(Long.parseLong(userid),null,null,null,null,null,null,null,null,null,null,null,new BigDecimal(0),null,null,null,0,0);
        TbTask tbTask = new TbTask(Long.parseLong(taskid),null,null,null,null,null,null,0,0,0,null,null,0);
        TbComment tbCom = new TbComment(comid,null,null,null,null,null,null,null);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        TbComment tbComment = new TbComment(text,null,df.format(new Date()),null,tbUser,tbTask,tbCom);
        int count = commentService.addComment(tbComment);
        Result result = new Result();
        result.setCode(count);
        return result;
    }

}
